package com.example.week12_networkprogramming;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import static android.widget.ListPopupWindow.WRAP_CONTENT;

public class HttpURLConnectionActivity extends AppCompatActivity {

    TextView showTextView=null;
    ImageView showimageView=null;
    String resultStr="";
    ProgressBar processBar=null;
    ViewGroup viewGroup=null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_http_urlconnection);

        showimageView=(ImageView)findViewById(R.id.imagview_show);
        showTextView=(TextView) findViewById(R.id.textview_show);

        findViewById(R.id.btn_visit_web).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showimageView.setVisibility(View.GONE);
                showTextView.setVisibility(View.VISIBLE);

           Thread visitBaiduThread=new Thread(new VisitWebRunnable());
                visitBaiduThread.start();
                try
                {
                    visitBaiduThread.join();
                    if(!resultStr.equals(""))
                    {
                        showTextView.setText(resultStr);
                    }
                }catch (InterruptedException e)
                {
                    e.printStackTrace();
                }
            }
        });

        findViewById(R.id.btn_download_img).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showimageView.setVisibility(View.VISIBLE);
                showTextView.setVisibility(View.GONE);
                String imgUrl="http://p3.itc.cn/q_70/images03/20200710/a31a2a2fb5434a438e39b443a53dca37.jpeg";
                new DownImgAsyncTask().execute(imgUrl);
            }
        });
    }

    class VisitWebRunnable implements Runnable
    {
        @Override
        public void run() {
            HttpURLConnection conn=null;
            InputStream is=null;
            String resultData="";
            try
            {
                URL url=new URL("https://m.baidu.com/");
                conn=(HttpURLConnection)url.openConnection();
                conn.setDoInput(true);
                conn.setDoOutput(true);
                conn.setUseCaches(false);
                conn.setRequestMethod("GET");
                is=conn.getInputStream();
                InputStreamReader isr=new InputStreamReader(is);
                BufferedReader bufferedReader=new BufferedReader(isr);
                String inputLine="";
                while((inputLine=bufferedReader.readLine())!=null)
                {
                    resultData+=inputLine+"\n";
                }

            }catch (MalformedURLException e)
            {
                e.printStackTrace();
            }catch (IOException e)
            {
                e.printStackTrace();
            }finally {
                if(is!=null)
                {
                    try {
                        is.close();
                    }catch (IOException e)
                    {
                        e.printStackTrace();
                    }
                }
                if(conn!=null)
                {
                    conn.disconnect();
                }
            }
            resultStr=resultData;
        }
    }
   class DownImgAsyncTask extends AsyncTask<String,Void,Bitmap>
   {
       @Override
       protected void onPreExecute() {
           super.onPreExecute();
           showimageView.setImageBitmap(null);
           showProgressBar();
       }

       @Override
       protected Bitmap doInBackground(String... params) {
           URL imgUrl=null;
           Bitmap bitmap=null;
           try
           {
               imgUrl=new URL(params[0]);
               HttpURLConnection conn=(HttpURLConnection)imgUrl.openConnection();
               conn.setDoInput(true);
               conn.setDoInput(true);
               conn.connect();
               InputStream is=conn.getInputStream();
               bitmap= BitmapFactory.decodeStream(is);
               is.close();
           }catch (MalformedURLException e)
           {
               e.printStackTrace();
           }catch (IOException e)
           {
               e.printStackTrace();
           }
           return bitmap;
       }

       @Override
       protected void onPostExecute(Bitmap result) {
           super.onPostExecute(result);
           if(result!=null)
           {
               dismissProgressBar();
               showimageView.setImageBitmap(result);
           }
       }
   }
   private void showProgressBar()
   {
       processBar =new ProgressBar(this,null,R.attr.progressBarStyle);
       RelativeLayout.LayoutParams params =new RelativeLayout.LayoutParams(WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);

       params.addRule(RelativeLayout.CENTER_IN_PARENT,RelativeLayout.TRUE);
       processBar.setVisibility(View.VISIBLE);
       Context context=getApplicationContext();
       viewGroup=(ViewGroup)findViewById(R.id.parent_view);
       viewGroup.addView(processBar,params);
   }

   private void dismissProgressBar()
   {
       if(processBar!=null)
       {
           processBar.setVisibility(View.GONE);
           viewGroup.removeView(processBar);
           processBar=null;
       }
   }

}
